package com.haoshuang.zhouzhoubang.ui.vm

import androidx.lifecycle.rxLifeScope
import com.haoshuang.zhouzhoubang.base.okRequest
import com.haoshuang.zhouzhoubang.base.request
import com.haoshuang.zhouzhoubang.model.bean.TeamGroupBean
import com.mufeng.libs.base.BaseViewModel
import com.mufeng.libs.state.StateData
import com.mufeng.libs.state.StatefulMutableLiveData


class GroupManagerViewModel : BaseViewModel() {


    /**
     * 获取门店分组列表
     */
    val getTeamGroupData = StatefulMutableLiveData<MutableList<TeamGroupBean>>()
    fun getTeamGroup(vararg params: Pair<String, Any?>) {
        rxLifeScope.launch {
            request<MutableList<TeamGroupBean>>("api/team/get_team_group_list", *params,
                onStart = {
                    getTeamGroupData.value = StateData.Loading
                },
                onError = { code, msg ->
                    getTeamGroupData.value = StateData.Error(code, msg)
                }, onSuccess = {
                    getTeamGroupData.value = StateData.Success(it)
                })
        }
    }

    /**
     * 创建分组
     */
    val createGroupData = StatefulMutableLiveData<Boolean>()
    fun createGroup(vararg params: Pair<String, Any?>) {
        rxLifeScope.launch {
            okRequest("api/team/create_team_group", *params,
                onStart = {
                    createGroupData.value = StateData.Loading
                },
                onError = { code, msg ->
                    createGroupData.value = StateData.Error(code, msg)
                }, onSuccess = {
                    createGroupData.value = StateData.Success(true)
                })
        }
    }

    /**
     * 修改分组
     */
    val EditGroupNameData = StatefulMutableLiveData<Boolean>()
    fun EditGroupName(vararg params: Pair<String, Any?>) {
        rxLifeScope.launch {
            okRequest("api/team/edit_team_group_name", *params,
                onStart = {
                    EditGroupNameData.value = StateData.Loading
                },
                onError = { code, msg ->
                    EditGroupNameData.value = StateData.Error(code, msg)
                }, onSuccess = {
                    EditGroupNameData.value = StateData.Success(true)
                })
        }
    }

    /**
     * 删除分组
     */
    val delGroupData = StatefulMutableLiveData<Boolean>()
    fun delGroup(vararg params: Pair<String, Any?>) {
        rxLifeScope.launch {
            okRequest("api/team/del_team_group", *params,
                onStart = {
                    delGroupData.value = StateData.Loading
                },
                onError = { code, msg ->
                    delGroupData.value = StateData.Error(code, msg)
                }, onSuccess = {
                    delGroupData.value = StateData.Success(true)
                })
        }
    }

    /**
     * 添加分组成员
     */
    val addGroupMemberData = StatefulMutableLiveData<Boolean>()
    fun addGroupMember(vararg params: Pair<String, Any?>) {
        rxLifeScope.launch {
            okRequest("api/team/add_team_group_member", *params,
                onStart = {
                    addGroupMemberData.value = StateData.Loading
                },
                onError = { code, msg ->
                    addGroupMemberData.value = StateData.Error(code, msg)
                }, onSuccess = {
                    addGroupMemberData.value = StateData.Success(true)
                })
        }
    }

    /**
     * 移出成员
     */
    val removeGroupMemberData = StatefulMutableLiveData<Boolean>()
    fun removeGroupMember(vararg params: Pair<String, Any?>) {
        rxLifeScope.launch {
            okRequest("api/team/del_team_group_member", *params,
                onStart = {
                    removeGroupMemberData.value = StateData.Loading
                },
                onError = { code, msg ->
                    removeGroupMemberData.value = StateData.Error(code, msg)
                }, onSuccess = {
                    removeGroupMemberData.value = StateData.Success(true)
                })
        }
    }

}